<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="/common/user.jsp"%>
<%!
	// 验证时间格式
	public static boolean isValidDate(String str) {
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = (Date) formatter.parse(str);
			return str.equals(formatter.format(date));
		} catch (Exception e) {
			return false;
		}
	}

%>
<%
	String checkNumber = input.getDesc("check_number");
	String where = "";
	if (hospital != 999)
		where = " and hospital=" + hospital  ;
	List<PhysicalCheck> list = QBuilder.db("basedb").queryHql("from PhysicalCheck where  checkNumber ='" + checkNumber + "'" + where + " and status=" + STATUS_ENABLE + " order by parentId asc,drawOrder asc" );
	if (list.size() == 0 ) {
		msg.put("data", Collections.emptyMap());
		Tools.encryptPrint(out, msg);
		return;
	}
	Map<String, String> util = new HashMap<String, String>();
	for(int i = 0; i < list.size(); i++) {
		PhysicalCheck pc = list.get(i);
		if (Tools.getInt(pc.getDrawOrder()) == -1) {
			util.put(pc.getItemId(), pc.getItemName());
			list.remove(i);
		}
	}
	Set<String> set = util.keySet();
	Map summary = new HashMap();
	PhysicalCheck physicalCheck = list.get(0);
	summary.put("check_number", Tools.getDesc(physicalCheck.getCheckNumber(), ""));
	summary.put("check_date", Tools.getDesc(physicalCheck.getCheckDate(), ""));
	summary.put("unit_number", Tools.getDesc(physicalCheck.getUnitNumber(), ""));
	summary.put("unit_name", Tools.getDesc(physicalCheck.getUnitName(), ""));
	summary.put("name", Tools.getDesc(physicalCheck.getName(), ""));
	summary.put("suggestion", Tools.getDesc(physicalCheck.getSuggestion(), ""));
	summary.put("physical_summary", Tools.getDesc(physicalCheck.getPhysicalSummary(), ""));
	summary.put("card_number", Tools.getDesc(physicalCheck.getIdenditiyNumber(), ""));
	List<Map> report  = new ArrayList<Map>();
	for (String itemId:set) {
		List<Map> items = new ArrayList<Map>();
		Iterator<PhysicalCheck> ite = list.iterator();
		while (ite.hasNext()) {
			PhysicalCheck pc = ite.next();
			if (pc.getParentId() == null) {
				continue;
			}
			if (itemId.equals(pc.getParentId())) {
				Map map = JSONUtil.fromJson(JSONUtil.toJson(pc), new TypeReference<Map>(){});
				map.remove("json");
				map.remove("status");
				map.remove("dept");
				map.remove("create_time");
				map.remove("hospital");
				map.remove("id");
				map.remove("draw_order");
				map.remove("suggestion");
				map.remove("physical_summary");
				map.remove("check_number");
				map.remove("check_date");
				map.remove("unit_number");
				map.remove("unit_name");
				map.remove("name");
				map.remove("idenditiy_number");
				items.add(map);
				ite.remove();
			}
		}
		Collections.sort(items, new Comparator<Map>() {
			public int compare(Map o1, Map o2) {
				return Tools.getDesc(o1.get("item_id")).compareTo(Tools.getDesc(o2.get("item_id")));
			}
		});
		Map param = new HashMap();
		param.put("title", util.get(itemId));
		param.put("items", items);
		param.put("title_id", itemId);
		report.add(param);
	}
	Iterator<PhysicalCheck> ite = list.iterator();
	while(ite.hasNext()) {
		PhysicalCheck pc = ite.next();
		String _itemId = pc.getItemId();
		int num = 0;
		String _id = "";
		char[] chs = _itemId.toCharArray();
		for (String itemId:set) {
			char[] _chs = itemId.toCharArray();
			int count = chs.length > _chs.length ? _chs.length : chs.length;
			int _num = 0;
			for (int i=0; i< count; i++) {
				if (_chs[i] == chs[i])
					_num ++;
			}
			if (_num > num) {
				num = _num;
				_id = itemId;
			}
		}
		for (Map reportMap : report) {
			String titleId = reportMap.get("title_id") + "";
			if (_id.equals(titleId)) {
				List items = (List)reportMap.get("items");
				Map map = JSONUtil.fromJson(JSONUtil.toJson(pc), new TypeReference<Map>(){});
				map.remove("json");
				map.remove("status");
				map.remove("dept");
				map.remove("create_time");
				map.remove("hospital");
				map.remove("id");
				map.remove("draw_order");
				map.remove("suggestion");
				map.remove("physical_summary");
				map.remove("check_number");
				map.remove("check_date");
				map.remove("unit_number");
				map.remove("unit_name");
				map.remove("name");
				map.remove("idenditiy_number");
				map.put("item_unit", Tools.getDesc(map.get("item_unit")).replaceAll(" ", "").trim());
				map.put("abnormal_info", Tools.getDesc(map.get("abnormal_info")).replaceAll(" ", "").trim());
				items.add(map);
				ite.remove();
			}
		}
	}
	Collections.sort(report, new Comparator<Map>() {
		public int compare(Map o1, Map o2) {
			return Tools.getDesc(o1.get("title_id")).compareTo(Tools.getDesc(o2.get("title_id")));
		}
	});
	Map resMap = new HashMap();
	resMap.put("title", summary);
	resMap.put("report", report);
	msg.put("data", resMap);
	Tools.encryptPrint(out, msg.toJson());
%>